package com.lftea.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.lftea.dao.IProductClassDAO;
import com.lftea.dao.IProductDAO;
import com.lftea.dao.IProductUnitDAO;
import com.lftea.po.Product;
import com.lftea.po.ProductClass;
import com.lftea.po.ProductUnit;

@Repository("productDAOImpl")
public class ProductDAOImpl implements IProductDAO {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Resource(name = "productClassDAOImpl")
	private IProductClassDAO productClassDAOImpl;

	@Resource(name = "productUnitDAOImpl")
	private IProductUnitDAO productUnitDAOImpl;
	
	public List<Product> findAll() {
		String sql = "select * from Product";
		return jdbcTemplate.query(sql, new ProductMapper());
	}

	public Product findById(long productId) {
		String sql = "select * from Product where Product_ID=?";
		return (Product) jdbcTemplate.query(sql, new Object[] { productId },
				new ProductMapper());
	}

	public void remove(Product product) {
		long productId = product.getId();
		String sql = "delete Product where Product_ID=?";
		jdbcTemplate.update(sql, new Object[] { productId });
	}

	public void save(Product product) {
		long productId = product.getId();
		String name = product.getName();
		String batchNum = product.getBatchNum();
		long productClassId = product.getProductClass().getId();
		float price = product.getPrice();
		long productUnitId = product.getProductUnit().getId();
		String remark = product.getRemark();

		String sql = "select count(*) from Product where Product_ID=?";
		int result = jdbcTemplate.queryForInt(sql);
		Object[] params = null;
		if (result > 0) {
			sql = "update Product set Name=?,BatchNum=?,ProductClass_ID=?,Price=?"
					+ ",productUnitId=?,remark=? where Product_ID=?";
			params = new Object[] { name, batchNum, productClassId, price,
					productUnitId, remark, productId };
		} else {
			sql = "insert into Product(Name,BatchNum,ProductClass_ID" +
					",Price,productUnitId,remark) values(?,?,?,?,?,?)";
			params = new Object[] { name, batchNum, productClassId,
					price, productUnitId, remark };
		}
		jdbcTemplate.update(sql, params);
	}

	private final class ProductMapper implements RowMapper<Product> {

		public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
			Product product = new Product();
			product.setId(rs.getLong("Product_ID"));
			product.setName(rs.getString("Name"));
			product.setBatchNum(rs.getString("BatchNum"));
			long productClassId = rs.getLong("ProductClass_ID");
			ProductClass productClass = 
				productClassDAOImpl.findById(productClassId);
			product.setProductClass(productClass);
			long productUnitId = rs.getLong("ProductUnit_ID");
			ProductUnit productUnit = productUnitDAOImpl.findById(productUnitId);
			product.setProductUnit(productUnit);
			product.setRemark(rs.getString("remark"));
			return product;
		}

	}
}
